define(function(require, exports) {
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var popwin = require('@base/dialog').base;
	var util = require('util');
	var view	= require('@base/view');

	var TYPE = {
		// 白名单
		'WHITE': 1,
		// 黑名单
		'BLACK': 2
	}

	var CreateGroup = view.widget.extend({
		init: function( config ) {
			config = pubjs.conf( config, {
				"url": 'adsGroup/add',
				"urlIds": 'spot/listByIds',
				"layout": {
					"module": '@base/dialog.base',
					"mask": 1,
					"title": LANG('添加广告位分组'),
					"showFoot": true, // 显示底层的取消和完成按钮
					"width": 500,
					"close": null,
					"silence": false, // 点击按钮发消息
					"size": 'm'
				},
				'view_model': {
					'Name': '',
					'ChannelName': '',
					'Type': TYPE.WHITE,
					'Opts': [
						{value: TYPE.WHITE, label: LANG("白名单")},
						{value: TYPE.BLACK, label: LANG("黑名单")}
					]
				}
			});
			this.Super( 'init', arguments );
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			$([
				'<div class="P-adsPopwinCreateGroup">',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('渠道：') + '</label>',
						'<span class="group-content">{{ChannelName}}</span>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('分组名称：') + '</label>',
						'<span class="group-content">',
							'<input ms-duplex-text="Name" class="w300"/>',
						'</span>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('分组类型：') + '</label>',
						'<span class="group-content">',
							'<label ms-repeat-type="Opts" class="mr20">',
								'<input type="radio" ms-duplex-text="Type" ms-value="type.value">',
								'<span class="ml5" ms-text="type.label" />',
							'</label>',
						'</span>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('已选广告位：') + '</label>',
						'<div class="group-content">',
							'<div class="spotContainer">',
								'<ul>',
								'</ul>',
							'</div>',
						'</div>',
					'</div>',
				'</div>'
			].join('')).appendTo(el);
			self.buildFromTemplate({
				
			});

			this.$spotCon = this.find('.spotContainer > ul');

		},
		onDialogCancel: function() {
			this.hide();
			return false;
		},
		onDialogOk: function() {
			this.save();
			return false;
		},
		save: function()
		{
			var self = this;
			var param = self.getValue();
			var url = this.getConfig('url');

			if (!param.Name)
			{
				pubjs.alert(LANG('广告位分组名称不能为空'));
				return false;
			}

			if (!param.AdxId)
			{
				pubjs.alert(LANG('渠道不能为空'));
				return false;
			}

			if (!param.Spots || !param.Spots.PositionIds || !param.Spots.PositionIds.length)
			{
				pubjs.alert(LANG('广告位不能为空'));
				return false;
			}


			pubjs.loading.show();
			pubjs.mc.send(url, param, function(err, data) {
				pubjs.loading.hide();
				if(err) 
				{
					pubjs.alert(err.message);
					return false;
				}
				pubjs.alert(LANG('保存成功'));
				self.reset().hide();
			});
		},
		setParam: function(param, replace) {
			this.$param = param;
			this.$AdxId = param.AdxId;
			this.$ids = (param.Spots && param.Spots.PositionIds) || [];

			var map = pubjs.config('channelMap');
			this.vm.$.ChannelName = map[this.$AdxId];

			// 获取广告位
			this.loadSpot();

			return this;
		},
		getValue: function()
		{
			var vm = this.vm.$;
			var result = {
				'Name': (vm.Name || '').trim(),
				'WhiteBlackType': vm.Type,
				'SyncCampaign': false,
				'AdxId': this.$AdxId,
				'Spots': {
					'PositionIds': this.$ids
				}
			};

			return result;
		},
		reset: function()
		{
			this.vm.reset();
			this.$param = null;
			this.$AdxId = null;
			this.$ids = [];
			return this;
		},

		// 广告位相关
		loadSpot: function() {
			var self = this;
			pubjs.mc.send(this.getConfig('urlIds'), {
				'ChannelId': self.$AdxId,
				'ids': self.$ids.join(',')
				,'no_stastic_data':1
				,'no_limit':1
				,'mongoField': ['_id', 'Name']
			}, self.afterLoadSpot.bind(this));
		},
		afterLoadSpot: function(err, data) {
			if(err) 
			{
				pubjs.alert(err.message);
				return false;
			}
			this.buildSpotItems(data.items);
		},
		buildSpotItems: function(items)
		{
			var selectItems = items || [];
			var el = [];
			var ids = [];
			util.each(selectItems, function(item, index)
			{
				el.push('<li>'+ item.Name +'</li>')
				ids.push(item._id);
			});

			this.$spotCon.html(el.join(''));
			this.$ids = ids;
		}
	});
	exports.base = CreateGroup;

});